package ca.polymtl.rubikcube.ColorCorrectionActivity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.Spinner;
import android.widget.TableLayout;
import android.widget.TableRow;
import ca.polymtl.rubikcube.util.Color;

public class ColorCorrectionActivity extends Activity implements OnClickListener, android.content.DialogInterface.OnClickListener {
	final private static String LOG_TAG = ColorCorrectionActivity.class.getSimpleName();
	
	Button buttons[][] = new Button[3][3];
	Spinner spinners[][] = new Spinner[3][3];
	int[] colors;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Intent intent = getIntent();
		colors = intent.getIntArrayExtra("ca.polymtl.rubikcube.colors");
		
		Log.d(LOG_TAG, "onCreate");
		
		if (colors == null) {
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setMessage("I need some colors !").setNeutralButton("Ok", this);
			builder.create().show();
		}
		
		LinearLayout layout = new LinearLayout(this);
		layout.setOrientation(LinearLayout.VERTICAL);
		
		LinearLayout.LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		params.gravity = Gravity.CENTER;
		Button doneBtn = new Button(this);
		doneBtn.setText("Done");
		doneBtn.setOnClickListener(this);
		layout.addView(doneBtn, params);
		
		TableLayout tableLayout = new TableLayout(this);
		
		ArrayAdapter<Color> arrayAdapter = new ArrayAdapter<Color>(this, android.R.layout.simple_spinner_item, Color.colorsById);
		arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		
		
		for (int j = 0; j < 3; j++) {
			TableRow row = new TableRow(this);
			for (int i = 0; i < 3; i++) {
				spinners[i][j] = new Spinner(this);
				spinners[i][j].setAdapter(arrayAdapter);
				spinners[i][j].setSelection(colors[j * 3 + i]);
				//buttons[i][j] = new Button(this);
				//buttons[i][j].setText(Color.getColorById(colors[j * 3 + i]).name);
				//row.addView(buttons[i][j]);
				row.addView(spinners[i][j]);
			}
			tableLayout.addView(row);
		}
		
		layout.addView(tableLayout, params);
		
		this.setContentView(layout);		
	}

	public void done() {
		
		Intent resultIntent = new Intent();
		
		for (int j = 0; j < 3; j++) {
			for (int i = 0; i < 3; i++) {
				colors[j * 3 + i] = spinners[i][j].getSelectedItemPosition();
			}
		}
		
		resultIntent.putExtra("ca.polymtl.rubikcube.colorsCorrected", colors);
		
		setResult(RESULT_OK, resultIntent);
		
		finish();
	}

	public void onClick(View view) {
		done();
	}

	public void onClick(DialogInterface dialog, int which) {
		finish();
	}
	
	@Override
	public void onBackPressed() {
		done();
	}
}
